package com.ruoyi.platform.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.platform.domain.MarketingActivity;
import com.ruoyi.platform.service.IMarketingActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 营销活动Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/platform/activity")
public class MarketingActivityController extends BaseController
{
    @Autowired
    private IMarketingActivityService marketingActivityService;

    /**
     * 查询营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(MarketingActivity marketingActivity)
    {
        startPage();
        List<MarketingActivity> list = marketingActivityService.selectMarketingActivityList(marketingActivity);
        return getDataTable(list);
    }

    /**
     * 导出营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:export')")
    @Log(title = "营销活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MarketingActivity marketingActivity)
    {
        List<MarketingActivity> list = marketingActivityService.selectMarketingActivityList(marketingActivity);
        ExcelUtil<MarketingActivity> util = new ExcelUtil<MarketingActivity>(MarketingActivity.class);
        util.exportExcel(response, list, "营销活动数据");
    }

    /**
     * 获取营销活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") String activityId)
    {
        return success(marketingActivityService.selectMarketingActivityByActivityId(activityId));
    }

    /**
     * 新增营销活动
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:add')")
    @Log(title = "营销活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MarketingActivity marketingActivity)
    {
        return toAjax(marketingActivityService.insertMarketingActivity(marketingActivity));
    }

    /**
     * 修改营销活动
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:edit')")
    @Log(title = "营销活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MarketingActivity marketingActivity)
    {
        return toAjax(marketingActivityService.updateMarketingActivity(marketingActivity));
    }

    /**
     * 删除营销活动
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:remove')")
    @Log(title = "营销活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable String[] activityIds)
    {
        return toAjax(marketingActivityService.deleteMarketingActivityByActivityIds(activityIds));
    }

    /**
     * 根据商家ID查询营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:list')")
    @GetMapping("/merchant/{merchantId}")
    public AjaxResult getByMerchantId(@PathVariable("merchantId") String merchantId)
    {
        List<MarketingActivity> list = marketingActivityService.selectMarketingActivityByMerchantId(merchantId);
        return success(list);
    }

    /**
     * 根据审核状态查询营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:list')")
    @GetMapping("/audit/{auditStatus}")
    public AjaxResult getByAuditStatus(@PathVariable("auditStatus") Integer auditStatus)
    {
        List<MarketingActivity> list = marketingActivityService.selectMarketingActivityByAuditStatus(auditStatus);
        return success(list);
    }

    /**
     * 根据商家ID和审核状态查询营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:list')")
    @GetMapping("/merchant/{merchantId}/audit/{auditStatus}")
    public AjaxResult getByMerchantIdAndAuditStatus(@PathVariable("merchantId") String merchantId, 
                                                   @PathVariable("auditStatus") Integer auditStatus)
    {
        List<MarketingActivity> list = marketingActivityService.selectMarketingActivityByMerchantIdAndAuditStatus(merchantId, auditStatus);
        return success(list);
    }

    /**
     * 审核通过营销活动
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:audit')")
    @Log(title = "营销活动审核", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{activityId}")
    public AjaxResult approve(@PathVariable("activityId") String activityId, @RequestBody Map<String, String> params)
    {
        String auditRemark = params.get("reason");
        return toAjax(marketingActivityService.approveMarketingActivity(activityId, auditRemark));
    }

    /**
     * 审核拒绝营销活动
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:audit')")
    @Log(title = "营销活动审核", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{activityId}")
    public AjaxResult reject(@PathVariable("activityId") String activityId, @RequestBody Map<String, String> params)
    {
        String rejectReason = params.get("reason");
        return toAjax(marketingActivityService.rejectMarketingActivity(activityId, rejectReason));
    }

    /**
     * 检查活动编码是否唯一
     */
    @GetMapping("/checkActivityCodeUnique")
    public AjaxResult checkActivityCodeUnique(String activityCode)
    {
        String result = marketingActivityService.checkActivityCodeUnique(activityCode);
        return success(result);
    }

    /**
     * 导入营销活动数据
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:import')")
    @Log(title = "营销活动", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<MarketingActivity> util = new ExcelUtil<MarketingActivity>(MarketingActivity.class);
        List<MarketingActivity> activityList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = marketingActivityService.importMarketingActivity(activityList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载营销活动导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MarketingActivity> util = new ExcelUtil<MarketingActivity>(MarketingActivity.class);
        util.importTemplateExcel(response, "营销活动数据");
    }

    /**
     * 获取营销活动统计信息
     */
    @PreAuthorize("@ss.hasPermi('platform:activity:list')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        Map<String, Object> statistics = new HashMap<>();
        
        // 查询各种状态的活动数量
        List<MarketingActivity> pendingList = marketingActivityService.selectMarketingActivityByAuditStatus(0);
        List<MarketingActivity> approvedList = marketingActivityService.selectMarketingActivityByAuditStatus(1);
        List<MarketingActivity> rejectedList = marketingActivityService.selectMarketingActivityByAuditStatus(2);
        
        statistics.put("pendingCount", pendingList.size());
        statistics.put("approvedCount", approvedList.size());
        statistics.put("rejectedCount", rejectedList.size());
        statistics.put("totalCount", pendingList.size() + approvedList.size() + rejectedList.size());
        
        return success(statistics);
    }
}